clear all;
close all;
%r4r6_data_raw1=importdata('g:\Research_Data\talinr4r6\20140914_talinwtr4r6_bead001_s1_tbp_0.4pns_4pns_alternative_c180-f-unfoldingstep.dat');
%r4r6_data_raw2=importdata('g:\Research_Data\talinr4r6\20140920_talinwtr4r6_bead004_s1_tbp_0.4pns_4pns_alternative_c155-f-unfoldingstep.dat');
%r4r6_data_raw3=importdata('g:\Research_Data\talinr4r6\20140920_talinwtr4r6_bead003_s1_tbp__0.4pns_4pns_alternative_c176-f-unfoldingstep.dat');


%Plot the unfolding force and contour length histograms.
r4r6_data_raw=importdata('g:\Research_Data\talin_F3_R1\goodunfolding.txt');
save_folder='G:\Mingxi\Dropbox\Mingxi-files\FL talin\figures\mechanical responses\';

%r4r6_data=[r4r6_data_raw1.data;r4r6_data_raw2.data;r4r6_data_raw4.data];
r4r6_data=r4r6_data_raw.data;
r4r6_slow=r4r6_data(mod(r4r6_data(:,1),2)==1,:);
r4r6_fast=r4r6_data(mod(r4r6_data(:,1),2)==0,:);
% %delete unfolding step of halo tag
% r4r6_slow=r4r6_slow(r4r6_slow(:,5)>30 & r4r6_slow(:,3)<35,:);
% r4r6_fast=r4r6_fast(r4r6_fast(:,5)>30 & r4r6_fast(:,3)<35,:);
% %
r4r6_fast_force_bin=histc(r4r6_fast(:,3),0:1:30);
r4r6_slow_force_bin=histc(r4r6_slow(:,3),0:1:30);
r4r6_fast_contour_bin=histc(r4r6_fast(:,5),0:5:150);
r4r6_slow_contour_bin=histc(abs(r4r6_slow(:,5)),0:5:150);
%cd 'G:\Mingxi\Dropbox\Thesis Proposal\codes\trunk\parameterfiting'

r4r6_fast_force_hist=[0:1:30; r4r6_fast_force_bin'];
r4r6_slow_force_hist=[0:1:30; r4r6_slow_force_bin'];
r4r6_fast_contour_hist=[0:5:150; r4r6_fast_contour_bin'];
r4r6_slow_contour_hist=[0:5:150; r4r6_slow_contour_bin'];
force_hist_fig=figure();
%setappdata(gcf, 'SubplotDefaultAxesLocation', [0, 0, 1, 1]);
subplot(2,1,1)
bar(r4r6_slow_force_hist(1,:),r4r6_slow_force_hist(2,:),'EdgeColor','k','FaceColor',[0.5 0.5 0.5],'LineWidth',1);
axis([0 30 0 80]);
set(gca,'FontSize',18,'LineWidth',2);
title('Talin F3-R1 domain','FontSize',20);
h_legend=legend('0.4 pN/s');
set(h_legend,'FontSize',18);
ylabel('Count','fontsize',22);
subplot(2,1,2);
bar(r4r6_fast_force_hist(1,:),r4r6_fast_force_hist(2,:),'EdgeColor','k','FaceColor',[0.5 0.5 0.5],'LineWidth',1);
axis([0 30 0 80]);
set(gca,'FontSize',18,'LineWidth',2);
h_legend=legend('3.4 pN/s');
set(h_legend,'FontSize',18);
xlabel('Unfolding Force (pN)','FontSize',22);
ylabel('Count','fontsize',22);
export_fig(sprintf('%s%s',save_folder,'talinf3_r1_unfolding_force_histogram.pdf'),'-transparent');
export_fig(sprintf('%s%s',save_folder,'talinf3_r1_unfolding_force_histogram.png'),'-transparent','-m2');

contour_hist_fig=figure();
subplot(2,1,1)
bar(r4r6_slow_contour_hist(1,:),r4r6_slow_contour_hist(2,:),'EdgeColor','k','FaceColor',[0.5 0.5 0.5],'LineWidth',1);
axis([0 150 0 100]);
set(gca,'FontSize',18,'LineWidth',2);
title('Talin F3-R1 domain','FontSize',20);
h_legend=legend('0.4 pN/s');
set(h_legend,'FontSize',18);
ylabel('Count','fontsize',22);
subplot(2,1,2)
bar(r4r6_fast_contour_hist(1,:),r4r6_fast_contour_hist(2,:),'EdgeColor','k','FaceColor',[0.5 0.5 0.5],'LineWidth',1);
axis([0 150 0 100]);
set(gca,'FontSize',18,'LineWidth',2);
h_legend=legend('3.4 pN/s');
set(h_legend,'FontSize',18);
xlabel('Unfolding Contour Length (nm)','FontSize',22);
ylabel('Count','fontsize',22);
export_fig(sprintf('%s%s',save_folder,'talinf3_r1_unfolding_contour_histogram.png'),'-transparent','-m2');
export_fig(sprintf('%s%s',save_folder,'talinf3_r1_unfolding_contour_histogram.pdf'),'-transparent');
%%
figure()
scatter(r4r6_fast(:,3),r4r6_fast(:,5),'b');
hold on;
scatter(r4r6_slow(:,3),r4r6_slow(:,5),'r');

%% Plot the force extension curves

%For alternative cycle measurements, odd cyles is around 0.4 pn/s even
%cycles around 4 pn/s, but always check
plotcycles=[1 3 5 9 11 13]
%plotcycles=[2 4 16 14 12 8]
c=180;
position_overstretching=8;
startforce=c*(exp(-(13.5-position_overstretching)/0.36)+0.48*exp(-(13.5-position_overstretching)/1.12));
first_unfolding=importdata('G:\Research_Data\talin_F3_R1\20141109_talin_f3r1_glusur_trisbsa_0.4pns_4pns_alternative_c180-cycle-unfoldingdata.dat','\t');
first_unfolding_data=first_unfolding.data;

h=figure();
%plotColor = linspecer(length(plotcycles'),'qualitative') ; 
% plotColor=[140 81 10;
% 216,179,101;
% 246,232,195;
% 199,234,229;
% 90,180,172;
% 1,102,94;]./255;
plotColor = lines(length(plotcycles')); 
for i=1:size(plotcycles')
plotcycle_data{i}=first_unfolding_data(first_unfolding_data(:,5)==plotcycles(i),:);
smoothedY=supsmu((plotcycle_data{i}(:,1)-plotcycle_data{i}(1,1)),plotcycle_data{i}(:,4),'Span',0.01);
%a=axis('tight');
loadingrate_fit=polyfit(plotcycle_data{i}(:,1),plotcycle_data{i}(:,3),1);
plot(((plotcycle_data{i}(:,1)-plotcycle_data{i}(1,1)).*loadingrate_fit(1)+startforce),smoothedY,'Color',plotColor(i,:),'LineWidth',2,'LineSmoothing','on');

hold on;
end
loadingrate_fit=polyfit(plotcycle_data{1}(:,1),plotcycle_data{1}(:,3),1);
title(sprintf('%s%3.1f%s','Talin F3-R1 domain ',loadingrate_fit(1),' pN/s'),'FontSize',20);
xlabel('Force (pN) ','fontsize',22);
ylabel('Extension (nm)','fontsize',22);
axis([0 25 0 180]);
set(gca,'FontSize',18,'LineWidth',2);
h_legend=legend('cycle1','cycle2','cycle3','cycle4','cycle5','cycle6');
set(h_legend,'FontSize',18,'Location','NorthWest');
export_fig(sprintf('%s%s%3.1f%s',save_folder,'talinf3_r1_force_extension_curve_',loadingrate_fit(1),'pNs.png'),'-transparent','-m2');
export_fig(sprintf('%s%s%3.1f%s',save_folder,'talinf3_r1_force_extension_curve_',loadingrate_fit(1),'pNs.pdf'),'-transparent');

 
 





